home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_313 / uucp / uucp1.lzh / src / compress / uuencode.c < prev   
C/C++ Source or Header  |  1989-11-03  |  2KB  |  145 lines

  1.  
  2. /*
  3.  * uuencode [input] output
  4.  *
  5.  * Encode a file so it can be mailed to a remote system.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include "/version.h"
  10.  
  11. IDENT(".00");
  12.  
  13. #ifdef UNIX
  14. # include <sys/types.h>
  15. # include <sys/stat.h>
  16. #endif
  17.  
  18. #ifdef VMS
  19. # include <types.h>
  20. # include <stat.h>
  21. #endif
  22.  
  23. /* ENC is the basic 1 character encoding function to make a char printing */
  24. #define ENC(c) (((c) & 077) + ' ')
  25.  
  26. void outdec();
  27. void encode();
  28.  
  29. void
  30. main(argc, argv)
  31. char **argv;
  32. {
  33.     FILE *in;
  34. #ifdef UNIX | VMS
  35.     struct stat sbuf;
  36. #endif
  37.     int mode;
  38.  
  39.     /* optional 1st argument */
  40.     if (argc > 2) {
  41.         if ((in = fopen(argv[1], "r")) == NULL) {
  42.             perror(argv[1]);
  43.             exit(1);
  44.         }
  45.         argv++; argc--;
  46.     } else
  47.         in = stdin;
  48.  
  49.     if (argc != 2) {
  50.         printf("Usage: uuencode [infile] remotefile\n");
  51.         exit(2);
  52.     }
  53.  
  54.     /* figure out the input file mode */
  55. #ifdef UNIX | VMS
  56.     fstat(fileno(in), &sbuf);
  57.     mode = sbuf.st_mode & 0777;
  58. #endif
  59.  
  60. #ifdef AMIGA
  61.     mode = 0777;
  62. #endif
  63.     printf("begin %o %s\n", mode, argv[1]);
  64.  
  65.     encode(in, stdout);
  66.  
  67.     printf("end\n");
  68.     exit(0);
  69. }
  70.  
  71. /*
  72.  * copy from in to out, encoding as you go along.
  73.  */
  74.  
  75. void
  76. encode(in, out)
  77. FILE *in;
  78. FILE *out;
  79. {
  80.     char buf[80];
  81.     int i, n;
  82.  
  83.     for (;;) {
  84.         /* 1 (up to) 45 character line */
  85.         n = fr(in, buf, 45);
  86.         putc(ENC(n), out);
  87.  
  88.         for (i=0; i<n; i += 3)
  89.             outdec(&buf[i], out);
  90.  
  91.         putc('X', out);
  92.         putc('\n', out);
  93.  
  94.         if (n <= 0)
  95.             break;
  96.     }
  97. }
  98.  
  99. /*
  100.  * output one group of 3 bytes, pointed at by p, on file f.
  101.  */
  102. void
  103. outdec(p, f)
  104. char *p;
  105. FILE *f;
  106. {
  107.     int c1, c2, c3, c4;
  108.  
  109.     c1 = *p >> 2;
  110.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  111.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  112.     c4 = p[2] & 077;
  113.     putc(ENC(c1), f);
  114.     putc(ENC(c2), f);
  115.     putc(ENC(c3), f);
  116.     putc(ENC(c4), f);
  117. }
  118.  
  119. /* fr: like read but stdio */
  120. int
  121. fr(fd, buf, cnt)
  122. FILE *fd;
  123. char *buf;
  124. int cnt;
  125. {
  126.     int c, i;
  127.  
  128.     for (i=0; i<cnt; i++) {
  129.         c = getc(fd);
  130.         if (c == EOF)
  131.             return(i);
  132.         buf[i] = c;
  133.     }
  134.     return (cnt);
  135. }
  136.  
  137. #ifdef AMIGA
  138. perror(err)
  139. char *err;
  140. {
  141.     printf("Can not open file \"%s\"\n", err);
  142.     return(NULL);
  143. }
  144. #endif
  145.